using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using MM.Entities;

namespace MM.Business
{
    class clsCHUC_DANH
    {
        public clsCHUC_DANH(string sConn)
	    {
		    SqlHelper.ConnectionString = sConn;
	    }
        public List<CHUC_DANH> DataTableToLstObj(DataTable dt)
        {
            CHUC_DANH cdDTO;
            List<CHUC_DANH> lstCdDTO = new List<CHUC_DANH>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cdDTO = new CHUC_DANH();
                try
                {
                    cdDTO._maChucDanh = int.Parse(dt.Rows[i]["MaChucDanh"].ToString());
                }
                catch (Exception ex)
                {
                }
                try
                {
                    cdDTO._tenChucDanh = dt.Rows[i]["TenChucDanh"].ToString();
                }
                catch (Exception ex)
                {
                }
                lstCdDTO.Add(cdDTO);
            }
            return lstCdDTO;
        }
        public CHUC_DANH DataTableToObj(DataTable dt)
        {
            CHUC_DANH cdDTO = new CHUC_DANH();
            try
            {
                cdDTO._maChucDanh =int.Parse(dt.Rows[0]["MaChucDanh"].ToString());
            }
            catch (Exception ex)
            {
            }
            try
            {
                cdDTO._tenChucDanh = dt.Rows[0]["TenChucDanh"].ToString();
            }
            catch (Exception ex)
            {
            }
            return cdDTO;
        }
        public List<CHUC_DANH> LoadChucDanh()
        {
            DataTable dt = new DataTable();
            List<CHUC_DANH> lstCdDTO = new List<CHUC_DANH>();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_SelectCHUC_DANHsAll";
                dt = SqlHelper.ExecuteToDataTable(spName, CommandType.StoredProcedure);
                lstCdDTO = DataTableToLstObj(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lstCdDTO;
        }

        public CHUC_DANH LoadChucDanhByMaCD(int maCD)
        {
            DataTable dt = new DataTable();
            CHUC_DANH cdDTO = new CHUC_DANH();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_SelectCHUC_DANH";
                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@MaChucDanh", maCD);
                dt = SqlHelper.ExecuteToDataTable(spName, par);
                cdDTO = DataTableToObj(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return cdDTO;
        }

        public int InsertCHUC_DANH(string ten)
        {
            int result = 0;
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_InsertUpdateCHUC_DANH";
                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@TenChucDanh", ten);
                result = SqlHelper.ExecuteNonQuery(spName, par);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return result;
        }
    }
}
